package com.example.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author hlc
 * @version 1.0
 * @date 2022/10/22 19:06
 */
@Configuration
public class RabbitMqConfig {
    /**
     * 发送邮件交换机
     */
    public static final String DIRECT_EMAIL_EXCHANGE = "direct_email_exchange";
    /**
     * 注册邮件队列
     */
    public static final String REGISTER_EMAIL_QUEUE = "register.email.queue";
    public static final String REGISTER_EMAIL_ROUTING_KEY = "register.email";
    /**
     * 验证邮件队列
     */
    public static final String CHECK_EMAIL_QUEUE = "check.email.queue";
    public static final String CHECK_EMAIL_ROUTING_KEY = "check.email";

    @Bean
    public DirectExchange directEmailExchange() {
        return ExchangeBuilder
                .directExchange(DIRECT_EMAIL_EXCHANGE)
                .durable(true)
                .build();
    }

    @Bean
    public Queue registerEmailQueue() {
        return QueueBuilder
                .durable(REGISTER_EMAIL_QUEUE)
                .build();
    }

    @Bean
    public Queue checkEmailQueue() {
        return QueueBuilder
                .durable(CHECK_EMAIL_QUEUE)
                .build();
    }

    @Bean
    public Binding registerEmailQueueBindingDirectEmailExchange(Queue registerEmailQueue, DirectExchange directEmailExchange) {
        return BindingBuilder
                .bind(registerEmailQueue)
                .to(directEmailExchange)
                .with(REGISTER_EMAIL_ROUTING_KEY);
    }

    @Bean
    public Binding checkEmailQueueBindingDirectEmailExchange(Queue checkEmailQueue, DirectExchange directEmailExchange) {
        return BindingBuilder
                .bind(checkEmailQueue)
                .to(directEmailExchange)
                .with(CHECK_EMAIL_ROUTING_KEY);
    }
}
